package com.ruoyi.media.task;


import com.ruoyi.common.constant.MediaConstants;
import com.ruoyi.media.domain.Plans;
import com.ruoyi.media.service.IPlansService;
import com.ruoyi.media.service.impl.PlansServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;

/***
 * @Author 鳄鱼杆
 * @Date 2025/6/4 11:45
 */
@Component
@Slf4j
public class PlanTask {

    @Autowired
    private PlansServiceImpl plansService;

    // 每天凌晨 0 点执行（0 0 0 * * ?）
    @Scheduled(cron = "0 0 0 * * ?")
    public void executeAtMidnight() {
        log.debug("定时任务正在执行...");
        //1、获取未过期的计划列表
        Plans plans = new Plans();
        plans.setIsExpiry(MediaConstants.IS_EXPIRY_NO);
        List<Plans> plansList = plansService.selectPlansList(plans);
        //2、遍历计划列表，判断计划是否过期
         plansList = plansList.stream()
                .filter(item -> item.getEndDate().toLocalDate().isBefore(LocalDate.now())).collect(Collectors.toList());
        //3、如果计划过期，则将计划状态改为已过期
        //批量更新计划状态
        plansService.BatchUpdatePlans(plansList,MediaConstants.IS_EXPIRY_YES);

    }
}
